package com.bidevalution.business.vote;

import com.alibaba.druid.util.StringUtils;
import com.bidevalution.business.impl.BeExpertVoteServiceImpl;
import com.bidevalution.business.service.BeEvalutionOutcomeService;
import com.bidevalution.business.service.BeSectionExpertInfoService;
import com.bidevalution.business.service.BeSectionStepService;
import com.bidevalution.dao.entity.BeExpertVoteCallBack;
import com.bidevalution.dao.entity.BeSectionExpertInfo;
import com.bidevalution.dao.entity.BeSectionStep;
import com.bidevalution.dao.entity.VoteParams;
import com.qtp.core.model.Response;
import com.qtp.core.util.GsonUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
@Log4j2
@Component
public class CompositeVoteCheckCallback extends BeExpertVoteServiceImpl {
    @Autowired
    private BeEvalutionOutcomeService beEvalutionOutcomeService;
    @Autowired
    private BeSectionStepService beSectionStepService;
    @Autowired
    private BeSectionExpertInfoService beSectionExpertInfoService;

    @Override
    public Response toUpdateTheVote(List<BeExpertVoteCallBack> voteCallBackList) {
        String sectionId = null;
        String sectionStepId = null;
        String submitCode = null;
        String layoutCode = null;
        String expertId = null;
        for(BeExpertVoteCallBack beExpertVoteCallBack : voteCallBackList) {
            sectionId = beExpertVoteCallBack.getSectionId();
            sectionStepId = beExpertVoteCallBack.getSectionStepId();
            String voteParamsStr = beExpertVoteCallBack.getVoteParams();
            if (!StringUtils.isEmpty(voteParamsStr)) {
                VoteParams voteParams = GsonUtil.parseJsonWithGson(voteParamsStr, VoteParams.class);
                voteParams.setResult(beExpertVoteCallBack.getVoteResule());
                // 去掉分
                voteParams.setResult(voteParams.getResult().replaceAll("分",""));
                layoutCode = voteParams.getLayoutCode();
                submitCode = voteParams.getSubmitCode();
                beEvalutionOutcomeService.updateResult(voteParams);
            }
        }

        // 专家ID
        List<BeSectionExpertInfo> beSectionExpertInfos = beSectionExpertInfoService.getExpertListBySectionId(sectionId);

        // 执行子存储过程
        for (BeSectionExpertInfo item: beSectionExpertInfos) {
            expertId = item.getSeqId();
            beEvalutionOutcomeService.callProcedure("p_update_score_technology", sectionId, sectionStepId, submitCode, layoutCode, expertId);
        }

        //查找父类stepId
        BeSectionStep beSectionStep = beSectionStepService.getSectionStepBySeqId(sectionStepId);
        // 执行父存储过程
        String parentSectionStepId = beSectionStep.getSeqId();
        String parentLayoutCode = "Center";
//        for (BeSectionExpertInfo item: beSectionExpertInfos) {
//            expertId = item.getSeqId();
//            beEvalutionOutcomeService.callProcedure("p_save_summary", sectionId, parentSectionStepId, submitCode, parentLayoutCode, expertId);
//        }


        return Response.ok();
    }
}
